﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using KPI.OrganizationDomain.Infrastructure.DbManager;
using KPI.OrganizationDomain.Infrastructure.DbObject;
using KPI.OrganizationDomain.Model.Entity;
using KPI.OrganizationDomain.Model.RepositoryInterface;
using KPI.OrganizationDomain.Model.ValueObject;
using KPI.Utility;

namespace KPI.OrganizationDomain.Infrastructure
{
    public class OrganizationRepository : IOrganizationRepository
    {

        public List<Department> GetAllDepartment()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Creates the department.
        /// </summary>
        /// <param name="department">The department.</param>
        /// <returns></returns>
        public Department CreateDepartment(Department department)
        {
            DepartmentManager departmentManager = new DepartmentManager();
            DepartmentTab depDBO = new DepartmentTab(department);

            using (SqlConnection conn = DatabaseConnectionProvider.GetKPISqlDbConnection())
            {
                conn.Open();
                depDBO = departmentManager.CreateDepartment(depDBO, conn, null);
                conn.Close();
            }

            return DepartmentTab.FillDepartment(depDBO, department);
        }

        public Employee CreateEmployee(Employee employee)
        {
            EmployeeManager employeeManager = new EmployeeManager();
            EmployeeTab employeeTabDto = new EmployeeTab(employee);
   
            using (SqlConnection conn = DatabaseConnectionProvider.GetKPISqlDbConnection())
            {
                conn.Open();
                employeeTabDto = employeeManager.CreateEmployee(employeeTabDto, conn, null);
                conn.Close();
            }

            return EmployeeTab.FillEmployee(employeeTabDto, employee);
        }

        public DataTable SelectEmployeeLevel()
        {
            EmployeeManager employeeManager = new EmployeeManager();
            DataTable datLevel = new DataTable();
           
            using (SqlConnection conn = DatabaseConnectionProvider.GetKPISqlDbConnection())
            {
                conn.Open();
                datLevel = employeeManager.SelectEmployeeLevel(conn, null);
                conn.Close();            
            }

            return datLevel;

        }

        public DataTable SelectEmployeeTitle()
        {
            EmployeeManager employeeManager = new EmployeeManager();
            DataTable datTitle= new DataTable();

            using (SqlConnection conn = DatabaseConnectionProvider.GetKPISqlDbConnection())
            {
                conn.Open();
                datTitle = employeeManager.SelectEmployeeTitle(conn, null);
                conn.Close();
            }

            return datTitle;

        }

        public DataTable SelectEmployeeDirector()
        {
            EmployeeManager employeeManager = new EmployeeManager();
            DataTable datDirector = new DataTable();

            using (SqlConnection conn = DatabaseConnectionProvider.GetKPISqlDbConnection())
            {
                conn.Open();
                datDirector = employeeManager.SelectEmployeeDirector(conn, null);
                conn.Close();
            }

            return datDirector;

        }

    }
}
